package com.tarim.streetdance.api;

import com.alibaba.fastjson.JSONObject;
import com.tarim.streetdance.model.FileSystemWithBLOBs;
import com.tarim.streetdance.model.User;
import com.tarim.streetdance.service.FileSystemService;
import com.tarim.streetdance.util.Constants;
import com.tarim.streetdance.util.JsonResult;
import com.tarim.streetdance.util.RedisUtil;
import com.tarim.streetdance.util.UploadFileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@CrossOrigin
@RequestMapping("/api/file")
public class FileApi {
    @Autowired
    RedisUtil redisUtil;

    @Autowired
    FileSystemService fileSystemService;

    /**
     * 上传视频
     * @param file
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public JsonResult upload (MultipartFile file, HttpServletRequest request, HttpServletResponse response) {
        JsonResult jsonResult;
        try {
            Object object = redisUtil.get(request.getHeader("token"));
            if (object == null) {
                jsonResult = new JsonResult(Constants.STATUS_FAIL, "登录后可上传文件");
                return jsonResult;
            }
            JSONObject jsonObject = (JSONObject) object;
            User user = jsonObject.toJavaObject(User.class);
            FileSystemWithBLOBs fileSystemWithBLOBs = UploadFileUtil.upload(file);
            if (fileSystemWithBLOBs == null) {
                jsonResult = new JsonResult(Constants.STATUS_FAIL, "文件格式不支持！");
                return jsonResult;
            }
            fileSystemWithBLOBs.setUserId(user.getId());
            fileSystemWithBLOBs.setUserCode(user.getUserCode());
            fileSystemWithBLOBs.setUserName(user.getUserName());
            int i = fileSystemService.insertSelective(fileSystemWithBLOBs);
            FileSystemWithBLOBs fileInfo = new FileSystemWithBLOBs();
            fileInfo.setId(fileSystemWithBLOBs.getId());
            fileInfo.setFilepath(fileSystemWithBLOBs.getFilepath());
            fileInfo.setFilename(fileSystemWithBLOBs.getFilename());
            fileInfo.setUrl(fileSystemWithBLOBs.getUrl());
            if (i != 0) {
                jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "上传成功", fileInfo);
            }else {
                jsonResult = new JsonResult(Constants.STATUS_FAIL, "上传失败");
            }
        }catch (Exception e) {
            e.printStackTrace();
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "系统异常,请联系管理员");
        }
        return jsonResult;
    }

    /**
     * 上传视频
     * @param file
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/uploadImage", method = RequestMethod.POST)
    public JsonResult uploadImage (MultipartFile file, HttpServletRequest request, HttpServletResponse response) {
        JsonResult jsonResult;
        try {
            Object object = redisUtil.get(request.getHeader("token"));
            if (object == null) {
                jsonResult = new JsonResult(Constants.STATUS_FAIL, "登录后可上传文件");
                return jsonResult;
            }
            JSONObject jsonObject = (JSONObject) object;
            User user = jsonObject.toJavaObject(User.class);
            FileSystemWithBLOBs fileSystemWithBLOBs = UploadFileUtil.uploadImage(file);
            if (fileSystemWithBLOBs == null) {
                jsonResult = new JsonResult(Constants.STATUS_FAIL, "文件格式不支持！");
                return jsonResult;
            }
            fileSystemWithBLOBs.setUserId(user.getId());
            fileSystemWithBLOBs.setUserCode(user.getUserCode());
            fileSystemWithBLOBs.setUserName(user.getUserName());
            int i = fileSystemService.insertSelective(fileSystemWithBLOBs);
            FileSystemWithBLOBs fileInfo = new FileSystemWithBLOBs();
            fileInfo.setId(fileSystemWithBLOBs.getId());
            fileInfo.setFilepath(fileSystemWithBLOBs.getFilepath());
            fileInfo.setFilename(fileSystemWithBLOBs.getFilename());
            fileInfo.setUrl(fileSystemWithBLOBs.getUrl());
            if (i != 0) {
                jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "上传成功", fileInfo);
            }else {
                jsonResult = new JsonResult(Constants.STATUS_FAIL, "上传失败");
            }
        }catch (Exception e) {
            e.printStackTrace();
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "系统异常,请联系管理员");
        }
        return jsonResult;
    }
}
